package socket.bio;

import java.io.*;
import java.net.Socket;

/**
 * @description: 类描述
 * @author: Fesine
 * @createTime:2016/9/29 16:16
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2016/9/29 16:16
 */
public class Worker implements Runnable {

    private Socket socket;

    public Worker(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        BufferedReader br = null;
        PrintWriter pw = null;

        try {
            br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            pw = new PrintWriter(socket.getOutputStream());
            String question = br.readLine();//没有内容会阻塞
            while (!question.equals("over")) {
                String answer = getAnswer(question);
                pw.println(answer);
                question = br.readLine();
            }
            pw.println("over!");
            pw.flush();
            if (pw != null) {
                pw.close();
            }
            if (br != null) {
                br.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String getAnswer(String question) {
        String answer = null;
        switch (question) {
            case "who":
                answer = "my name is fesine";
                break;
            case "what":
                answer = "do IT!";
                break;
            case "where":
                answer = "from jiangsu wuxi";
                break;
            default:
                answer = "请输入who,what,where";
        }
        return answer;
    }

}
